package com.icity.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.icity.entity.ChannelActive;
import com.icity.entity.ChannelAddUser;
import com.icity.entity.ChannelStart;
import com.icity.entity.DurationInfos;
import com.icity.entity.Partnerstatistical;
import com.icity.entity.TrendsUser;
import com.icity.entity.UserDistribution;
import com.icity.entity.UserTrends;
import com.icity.entity.content.ArticleSummary;
import com.icity.entity.system.TableSupport;
import com.icity.mapper.TrendsUserMapper;
import com.icity.service.TrendsUserService;
import com.icity.util.DateUtils;

@Service
public class TrendsUserServiceImpl implements TrendsUserService {

	@Autowired
	private TrendsUserMapper trendsUserMapper;


	@Override
	public TableSupport<?> getLsit(Map<Object, Object> map){

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		TableSupport<TrendsUser> listTable = new TableSupport<TrendsUser>();
		List<TrendsUser> TrendsUserdata = trendsUserMapper.getLsit(map);
		for (TrendsUser trendsUser : TrendsUserdata) {
			trendsUser.setAdduser(trendsUser.getAdduser()*9);
			trendsUser.setStartnum(trendsUser.getStartnum()*9);
			trendsUser.setActiveuserday(trendsUser.getActiveuserday()*9);
			trendsUser.setActiveuserweek(trendsUser.getActiveuserweek()*9);
			trendsUser.setActiveusermonth(trendsUser.getActiveusermonth()*9);
			trendsUser.setDateCn(sdf.format(trendsUser.getDateof()));
		}
		int count = trendsUserMapper.selectCount(map);
		listTable.setCode(0);
		listTable.setMsg(null);
		listTable.setCount(count);
		listTable.setData(TrendsUserdata);
		return listTable;
	}

	@Override
	public TableSupport<?> getPartnertable(Map<Object, Object> map){
		TableSupport<Partnerstatistical> listTable = new TableSupport<Partnerstatistical>();
		List<Partnerstatistical> TrendsUserdata = trendsUserMapper.getPartnertable(map);
		int count = trendsUserMapper.selectpartnerCount(map);
		listTable.setCode(0);
		listTable.setMsg(null);
		listTable.setCount(count);
		listTable.setData(TrendsUserdata);
		return listTable;

	}


	@Override
	public List<String> getWeekDay(Map<Object, Object> mapDate, int days) {

		List<String> list = new ArrayList<String>();
		if (mapDate.containsKey("nowDate")) {
			String date = mapDate.get("nowDate").toString();
			for (int i = days-1; i >= 0; i--) {
				list.add(DateUtils.getSpecifiedDayBefore(date, i));
			}
		} else {
			days = (int) DateUtils.getDaySub(mapDate.get("startDate").toString(), mapDate.get("endDate").toString());
			for (int i = days; i >= 0; i--) {
				list.add(DateUtils.getSpecifiedDayBefore(mapDate.get("endDate").toString(), i));
			}
		}

		return list;
	}

	@Override
	public List<Integer> getdatanum(Map<Object, Object> params){
		List<ChannelActive> list=trendsUserMapper.getdatanum(params);
		List<Integer> num=new ArrayList<>();
		for(ChannelActive channelActive : list) {
			int nums=channelActive.getAli()+channelActive.getAnzhuo()+channelActive.getBaidu()+channelActive.getChuizi()+
					channelActive.getHuawei()+channelActive.getIcity()+channelActive.getLianxiang()+channelActive.getMeizu()
					+channelActive.getOppo()+channelActive.getShichang91()+channelActive.getYiyonghui()+channelActive.getZhushou360();
			num.add(nums);
		}

		return num ;
	}

	@Override
	public List<Integer> getaddusernum(Map<Object, Object> params){
		List<ChannelAddUser> list=trendsUserMapper.getaddusernum(params);
		List<Integer> num=new ArrayList<>();
		for(ChannelAddUser channelActive : list) {
			int nums=channelActive.getAli()+channelActive.getAnzhuo()+channelActive.getBaidu()+channelActive.getChuizi()+
					channelActive.getHuawei()+channelActive.getIcity()+channelActive.getLianxiang()+channelActive.getMeizu()
					+channelActive.getOppo()+channelActive.getShichang91()+channelActive.getYiyonghui()+channelActive.getZhushou360();
			num.add(nums);
		}
		return num;
	}

	@Override
	public List<Integer> getstartnum(Map<Object, Object> params){
		List<ChannelStart> list=trendsUserMapper.getstartnum(params);
		List<Integer> num=new ArrayList<>();
		for(ChannelStart channelActive : list) {
			int nums=channelActive.getAli()+channelActive.getAnzhuo()+channelActive.getBaidu()+channelActive.getChuizi()+
					channelActive.getHuawei()+channelActive.getIcity()+channelActive.getLianxiang()+channelActive.getMeizu()
					+channelActive.getOppo()+channelActive.getShichang91()+channelActive.getYiyonghui()+channelActive.getZhushou360();
			num.add(nums);
		}
		return num;
	}
	@Override
	public List<Integer> selectappuserman(){

		return trendsUserMapper.selectappuserman();
	}

	@Override
	public List<Integer> selectappuserwoman(){

		return trendsUserMapper.selectappuserwoman();
	}

	@Override
	public List<Integer> selectappuserunknown(){

		return trendsUserMapper.selectappuserunknown();
	}

	@Override
	public List<String> getUserDistribution(Map<Object, Object> map){
		return trendsUserMapper.getUserDistribution(map);

	}
	@Override
	public List<Integer> getUserProvinceNumDistribution(Map<Object, Object> map){
		return trendsUserMapper.getUserProvinceNumDistribution(map);

	}

	@Override
	public 	List<UserDistribution> getUserChinaDistribution(Map<Object, Object> map){

		return trendsUserMapper.getUserChinaDistribution(map);
	}

	@Override
	public 	List<String> getUsercityDistribution(Map<Object, Object> map){
		return trendsUserMapper.getUsercityDistribution(map);

	}

	@Override
	public List<Integer> getUserNumDistributioncity(Map<Object, Object> map){
		return trendsUserMapper.getUserNumDistributioncity(map);
	}

	@Override
	public UserTrends getUserTrends(String date) {
		UserTrends userTrends = trendsUserMapper.getUserTrend(date);
		userTrends.setAdd_user_seven(userTrends.getAdd_user_seven()*9);
		userTrends.setActive_user_seven(userTrends.getActive_user_seven()*9);
		userTrends.setAll_user_seven(userTrends.getAll_user_seven()*9);
		userTrends.setAll_user_thrity(userTrends.getAll_user_thrity()*9);
		userTrends.setTime_seven(userTrends.getTime_seven()*9);
		userTrends.setStart_seven(userTrends.getStart_seven()*9);
		userTrends.setAll_add_user(userTrends.getAll_add_user()*9);
		userTrends.setAll_active_user(userTrends.getAll_active_user()*9);
		return userTrends;
	}

	@Override
	public Map<Object, List<String>> getLegend() {
		return null;
	}

	@Override
	public Map<Object, String> getTooltip() {
		Map<Object, String> map = new HashMap<Object, String>();
		map.put("trigger", "axis");
		return map;
	}

	@Override
	public Map<Object, Object> getYaxis() {
		Map<Object, Object> map = new HashMap<Object, Object>();
		map.put("type", "value");
		return map;
	}

	@Override
	public Map<Object, Object> getGrid() {
		Map<Object, Object> map = new HashMap<Object, Object>();
		map.put("left", "3%");
		map.put("right", "4%");
		map.put("bottom", "3%");
		map.put("containLabel", true);
		return map;
	}

	@Override
	public Map<Object, Object> getWeekDayMap(Map<Object, Object> mapDate, int days) {
		Map<Object, Object> map = new HashMap<Object, Object>();
		List<String> list = new ArrayList<String>();
		if (mapDate.containsKey("nowDate")) {
			String date = mapDate.get("nowDate").toString();
			for (int i = days; i >= 1; i--) {
				list.add(DateUtils.getSpecifiedDayBefore(date, i));
			}
		} else {
			days = (int) DateUtils.getDaySub(mapDate.get("startDate").toString(), mapDate.get("endDate").toString());
			for (int i = days; i >= 0; i--) {
				list.add(DateUtils.getSpecifiedDayBefore(mapDate.get("endDate").toString(), i));
			}
		}
		map.put("type", "category");
		map.put("boundaryGap", false);
		map.put("data", list);
		return map;
	}

	@Override
	public List<Map<Object, Object>> getTrendsUserSeries(Map<Object, Object> map) {
		List<TrendsUser> listTrendUser = trendsUserMapper.getTrendsUserByType(map);
		List<Integer> list = new ArrayList<Integer>();
		String type = (String) map.get("type");
		switch (type) {
		case "newAdd":
			for (TrendsUser trendsUser : listTrendUser) {
				list.add(trendsUser.getAdduser()*9);
			}
			break;
		case "launch":
			for (TrendsUser trendsUser : listTrendUser) {
				list.add(trendsUser.getStartnum()*9);
			}
			break;
		case "avtiveDay":
			for (TrendsUser trendsUser : listTrendUser) {
				list.add(trendsUser.getActiveuserday()*9);
			}
			break;
		case "activeWeek":
			for (TrendsUser trendsUser : listTrendUser) {
				list.add(trendsUser.getActiveuserweek()*9);
			}
			break;
		case "activeMonth":
			for (TrendsUser trendsUser : listTrendUser) {
				list.add(trendsUser.getActiveusermonth()*9);
			}
			break;
		case "time":
			List<DurationInfos> durationInfosList = trendsUserMapper.getDurationInfos(map);
			for(DurationInfos durationInfos : durationInfosList){
				float average = Float.parseFloat(durationInfos.getAverage());
				list.add((int)average*9);
			}
			break;
		default:
			for (TrendsUser trendsUser : listTrendUser) {
				list.add(trendsUser.getAdduser()*9);
			}
			break;
		}
		List<Map<Object, Object>> listSeries = new ArrayList<Map<Object, Object>>();
		Map<Object, Object> mapOppo = new HashMap<Object, Object>();
		mapOppo.put("name", "数量");
		mapOppo.put("type", "line");
		mapOppo.put("stack", "总量");
		mapOppo.put("data", list);
		listSeries.add(mapOppo);
		return listSeries;
	}

}
